package com.kmist.jianghu;

import android.app.Activity;
import android.os.Handler;
import android.os.Message;

import java.util.Random;

public class Combate {

    private Monster monster;
    private Player player;

    public Combate(int area){
        //初始化一只怪物
        Random ra = new Random();
        int id = ra.nextInt(6) % 7;
        monster = MainActivity.monsterlib.getMonster(id);
        player = MainActivity.player;

    }

    public Monster getMonster() {
        return monster;
    }

    public void attackMonser(int attid){
        int curMonsterHp = monster.getmCurHp();

        if(attid == 1){
            curMonsterHp -= player.getmAttach();
            curMonsterHp = safe_data(curMonsterHp);
            monster.setmCurHp(curMonsterHp);
            return;
        }

        if(attid == 2){

            curMonsterHp -= player.getmAttach()*2;
            curMonsterHp = safe_data(curMonsterHp);
            monster.setmCurHp(curMonsterHp);
            return;
        }

        if(attid == 3){
            curMonsterHp -= player.getmAttach()*3;
            curMonsterHp = safe_data(curMonsterHp);
            monster.setmCurHp(curMonsterHp);
            return;
        }

        if(attid == 4){
            curMonsterHp -= player.getmAttach()*4;
            curMonsterHp = safe_data(curMonsterHp);
            monster.setmCurHp(curMonsterHp);
            return;
        }
    }

    private int safe_data(int data) {
        int data1 = data;
        if(data1 < 0){
            data1 = 0;
        }
        return data1;
    }

    public void start() {

        Thread battle = new Thread(){
            int player_cd = 0;
            int monster_cd = 0;
            int curPlayerHp;
            int curMonsterHp;
            @Override
            public void run() {

                curPlayerHp = player.getmHp();
                curMonsterHp = monster.getmHp();

                player_cd = player.getmSpeed();
                monster_cd = monster.getmSpeed();

                while(curPlayerHp > 0 && curMonsterHp > 0){

                    curMonsterHp = monster.getmCurHp();
                    try {
                        Thread.sleep(500);
                        attack_loop();
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }
            }

            private void attack_loop() {
                player_cd -= 10;
                monster_cd -= 10;

                if(player_cd <=0 ){
                    Random ra = new Random();
                    int buff = ra.nextInt(player.getmAttach()) % (player.getmAttach() + 1);
                    int hurt = player.getmAttach() + buff;
                    curMonsterHp -= hurt;
                    curMonsterHp = safe_data(curMonsterHp);
                    monster.setmCurHp(curMonsterHp);
                    Message msg = new Message();
                    msg.what = 1;
                    msg.arg1 = hurt;
                    JianHu.uiHandler.sendMessage(msg);
                    player_cd = player.getmSpeed();
                }

                if(monster_cd <= 0){
                    Random ra = new Random();
                    int buff = ra.nextInt(monster.getmAttack()) % (monster.getmAttack() + 1);
                    int hurt = player.getmAttach() + buff;
                    curPlayerHp -= hurt;
                    curPlayerHp = safe_data(curPlayerHp);
                    player.setmCurHp(curPlayerHp);

                    Message msg = new Message();
                    msg.what = 2;
                    msg.arg1 = hurt;
                    JianHu.uiHandler.sendMessage(msg);
                    monster_cd = monster.getmSpeed();
                }
                /* 死亡判断 */
                if(curPlayerHp == 0 || curMonsterHp == 0){
                    Message msg = new Message();
                    msg.what = 9;
                    if(curPlayerHp == 0){
                        msg.arg1 = 2;
                        player.die();
                    }else{
                        msg.arg1 = 1;
                        msg.arg2 = monster.getmJinbi();
                        monster.die();
                        Combate_diaoluo();
                    }
                    JianHu.uiHandler.sendMessage(msg);

                }
            }
        };
        battle.start();
    }

    public void Combate_diaoluo(){
        //jinbi
        player.setmJinbi(player.getmJinbi() + monster.getmJinbi());
        //jingyan
        player.setmEvolution(player.getmEvolution() + 100);

        //wupin,wuqi

    }
}
